package com.koocloud.electroplatemanage.service.impl;

import cn.hutool.core.lang.Validator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koocloud.electroplatemanage.common.pojo.ResponseTemplate;
import com.koocloud.electroplatemanage.constant.Constant;
import com.koocloud.electroplatemanage.mapper.PersonMapper;
import com.koocloud.electroplatemanage.pojo.DrawNoVo;
import com.koocloud.electroplatemanage.pojo.PersonVo;
import com.koocloud.electroplatemanage.service.PersonService;
import com.koocloud.electroplatemanage.utils.UUIDUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @program: electroplatemanage
 * @description: 人员服务层实现类
 * @author: zww
 * @create: 2020-09-30 15:33
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class PersonServiceImpl implements PersonService {
    @Resource
    private PersonMapper personMapper;
    Lock lock = new ReentrantLock();
    /**
     *@Description:通过pk查询一条记录
     *@Param: [pk]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: zww
     *@date: 2020/9/29
     */
    @Override
    public ResponseTemplate selectByPk(String pk) {
        try {
            //新建人员实体
            PersonVo personVo = null;
            personVo = personMapper.selectByPk(pk);
            //判断查表操作是否成功
            if (null != personVo) {
                //查表成功返回数据
                return ResponseTemplate.builder().code(0).message("查询成功").count(1l).data(personVo).build();
            }
            else {
                //查表失败返回提示
                return ResponseTemplate.builder().code(1).message("没有此数据").count(0l).build();
            }
        } catch (Exception e) {
            //遇到异常，返回异常原因
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }

    }
    /**
     *@Description:查询所有记录 分页
     *@Param: [page, limit]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: zww
     *@date: 2020/9/29
     */
    @Override
    public ResponseTemplate selectAllData(String page, String limit,PersonVo personVo) {
        //校验分页参数是否为纯数字 并且page和limit不都等于0
        if(Validator.isNumber(page)&&Validator.isNumber(limit)&&!("0".equals(page)&&"0".equals(limit))) {
            try {
                //开始分页
                PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(limit));
                List<PersonVo> PersonVoList = personMapper.selectAllData(personVo);
                //构造分页实体
                PageInfo<PersonVo> info=new PageInfo<>(PersonVoList);
                if (info.getTotal()>0) {
                    //查询到数据
                    return ResponseTemplate.builder().code(0).message("查询成功").count(info.getTotal()).data(info.getList()).build();
                } else {
                    //没有查询到数据
                    return ResponseTemplate.builder().code(1).message("没有查到对应数据").count(0l).build();
                }
            } catch (Exception e) {
                //遇到异常，返回异常原因
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
            }
        }
        else {
            return ResponseTemplate.builder().code(1).message("分页参数错误").count(0l).build();
        }
    }

    /**
     *@Description://插入一条记录
     *@Param: [personVo]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: zww
     *@date: 2020/9/29
     */
    @Override
    public ResponseTemplate insert(PersonVo personVo) {
        try {
            lock.lock();
            //没有异常并且插入数据条数大于0
            int rows = personMapper.insert(personVo);
            if(rows>0) {
                return ResponseTemplate.builder().code(0).message("插入数据成功").count(1l).build();
            }
            else {//没有异常并且插入数据失败，原则上不出现
                return ResponseTemplate.builder().code(1).message("插入数据失败").count(0l).build();
            }
        } catch (Exception e) {
            //出现异常返回异常原因
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }finally {
            lock.unlock();
        }
    }

    /**
     *@Description:批量插入记录
     *@Param: [listPersonVo]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: zww
     *@date: 2020/9/29
     */
    @Override
    public ResponseTemplate batchInsert(List<PersonVo> listPersonVo) {
        if(!listPersonVo.isEmpty()) {
            try {
                lock.lock();
                //没有异常并且插入数据条数大于0
                int rows = personMapper.batchInsert(listPersonVo);
                if (rows > 0) {
                    return ResponseTemplate.builder().code(0).message("批量插入数据成功").count((long)rows).build();
                } else {//没有异常并且插入数据失败，原则上不出现
                    return ResponseTemplate.builder().code(1).message("批量插入数据失败").count(0l).build();
                }
            } catch (Exception e) {
                //出现异常返回异常原因
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
            }finally {
                lock.unlock();
            }
        }
        else {
            return ResponseTemplate.builder().code(1).message("传入参数为空").count(0l).build();
        }
    }
    /**
     *@Description:逻辑删除一条记录
     *@Param: [pk]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: zww
     *@date: 2020/9/29
     */
    @Override
    public ResponseTemplate delete(String pk) {
        try {
            lock.lock();
            int rows = personMapper.delete(pk);
            //判断删除操作是否成功
            if (rows > 0) {
                //删除成功返回数据
                return ResponseTemplate.builder().code(0).message("删除成功").count(1l).build();
            }
            else {
                //删除失败返回提示
                return ResponseTemplate.builder().code(1).message("删除失败").count(0l).build();
            }
        } catch (Exception e) {
            //遇到异常，返回异常原因
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }finally {
            lock.unlock();
        }
    }
    /**
     *@Description:通过pks批量逻辑删除记录
     *@Param: [listPks]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: zww
     *@date: 2020/9/29
     */
    @Override
    public ResponseTemplate batchDeleteByPks(List<String> listPks) {
        if(!listPks.isEmpty()) {
            try {
                lock.lock();
                //存在合法pk值，进行批量删除操作
                int rows = personMapper.batchDeleteByPks(listPks);
                //判断批量删除操作是否成功
                if (rows > 0) {
                    //批量删除成功返回数据
                    return ResponseTemplate.builder().code(0).message("批量删除成功").count((long)rows).build();
                } else {
                    //批量删除失败返回提示
                    return ResponseTemplate.builder().code(1).message("批量删除失败").count(0l).build();
                }
            } catch (Exception e) {
                //遇到异常，返回异常原因
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
            }finally {
                lock.unlock();
            }
        }
        else {
            return ResponseTemplate.builder().code(1).message("传入参数为空").count(0l).build();
        }
    }
    /**
     *@Description:修改一条记录
     *@Param: [personVo]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: zww
     *@date: 2020/9/29
     */
    @Override
    public ResponseTemplate update(PersonVo personVo) {
        try {
            lock.lock();
            //没有异常并且修改数据条数大于0
            int rows = personMapper.update(personVo);
            if(rows>0) {
                return ResponseTemplate.builder().code(0).message("修改数据成功").count(1l).build();
            }
            else {//没有异常并且修改数据失败，原则上不出现
                return ResponseTemplate.builder().code(1).message("修改数据失败").count(0l).build();
            }
        } catch (Exception e) {
            //出现异常返回异常原因
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }finally {
            lock.unlock();
        }
    }


    @Override
    public ResponseTemplate check(PersonVo personVo) {
        try {
            log("传入填写的人员表主键："+personVo.getCode());

            if (!StringUtils.isEmpty(personVo.getCode())){ //判断传入的主键是否为空
                //校验主键的长度
                if (!(personVo.getCode().length()==Integer.parseInt(Constant.controlPrikeyLength))){

                    return ResponseTemplate.builder().code(1).message("填写的人员表主键必须长度为"+Constant.controlPrikeyLength+"位").count(0l).build();
                }else { //校验长度通过

                    //根据主键id查询一条数据
                    PersonVo personVo1 = personMapper.selectByPk(personVo.getCode());
                    if (!StringUtils.isEmpty(personVo1)){
                        return ResponseTemplate.builder().code(1).message("填写的人员表主键已存在,请重新输入").count(1l).build();
                    }else {
                        return ResponseTemplate.builder().code(0).message("此人员表主键可用").count(1l).build();
                    }
                }
            }else {
                return ResponseTemplate.builder().code(1).message("传入的人员表主键为空").count(1l).build();
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.toString()).count(0l).build();
        }

    }


    public void  log(String str){
        System.out.println("====================>"+str+"=================>");
    }
}